import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Manager.vue'
import store from "@/store";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import {Notification} from "element-ui";

Vue.use(VueRouter)
NProgress.configure({showSpinner: false });
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const routes = [{
    path: '/login', name: '登录', component: () => import('../views/Login.vue')
}, {
    path: "/register", name: "注册", component: () => import("../views/Register.vue")
},{
    path: "/retrieveAccount", name: "RetrieveAccount", component: () => import("../views/RetrieveAccount.vue")
},{
    path: "/frontRetrieveAccount", name: "FrontRetrieveAccount", component: () => import("../views/front/RetrieveAccount.vue")
},{
    path:"/frontLogin",name:"FrontLogin",component:()=>import("../views/front/Login.vue")
},{
    path:"/frontRegister",name:"FrontRegister",component:()=>import("../views/front/Register.vue")
},{
    path:"/front",name:"Front",redirect:'/front/home',component:()=>import("../views/front/Front.vue"),
    children:[{
        path:"home",name:"Home",component:()=>import("../views/front/Home.vue")
    },{
        path:"person",name:"FrontPerson",component:()=>import("../views/front/Person.vue")
    },{
        path:"password",name:"FrontPassword",component:()=>import("../views/front/Password.vue")
    },{
        path:"article",name:"FrontArticle",component:()=>import("../views/front/Article.vue")
    },{
        path:"articleDetail",name:"FrontArticleDetail",component:()=>import("../views/front/ArticleDetail.vue")
    },{
        path:"publish",name:"FrontPublish",component:()=>import("../views/front/Publish.vue")
    }]
},{
    path: "/404", name: '404', component: () => import('../views/404.vue')
}];

const router = new VueRouter({
    mode: 'history', base: process.env.BASE_URL, routes
})

export const resetRouter=()=>{
    router.matcher=new VueRouter({
        mode: 'history', base: process.env.BASE_URL, routes
    })
}
export const setRoutes = () => {
    const storeMenus = localStorage.getItem("menuList");
    if (storeMenus) {
        const currentRouteNames = router.getRoutes().map(route => route.name);
        if (!currentRouteNames.includes('Manager')) {
            const manageRoute = {
                path: "/",
                name: 'Manager',
                component: () => import('../views/Manager.vue'),
                redirect: '/home',
                children: [{
                    path: '/person', name: '个人信息', component: () => import('../views/Person.vue')
                }, {
                    path: "/password", name: "修改密码", component: () => import("../views/Password.vue")
                }]
            };
            const menuList = JSON.parse(storeMenus);
            menuList.forEach(item => {
                if (item.path) {
                    let menuItem = {
                        path: item.path.replace("/", ""),
                        name: item.menuName,
                        component: () => import('../views/' + item.pagePath + '.vue')
                    };
                    manageRoute.children.push(menuItem);
                } else if (item.submenuList.length) {
                    let  subMenuRoute={
                        path:"/".replace("/", ""),
                        name:item.menuName,
                        redirect: '/home',
                        children:[]
                    };
                    subMenuRoute.component={render(c) {
                            return c("router-view");
                        }}
                    item.submenuList.forEach(item => {
                        if (item.path) {
                            let menuItem = {
                                path: item.path.replace("/", ""),
                                name: item.menuName,
                                component: () => import('../views/' + item.pagePath + '.vue')
                            };
                           subMenuRoute.children.push(menuItem);
                        }else if (item.submenuList.length) {
                            let thirdMenuRoute={
                                path:"/".replace("/", ""),
                                name:item.menuName,
                                redirect: '/home',
                                children:[]
                            }
                            thirdMenuRoute.component={render(c) {
                                return c("router-view");
                                }}
                            item.submenuList.forEach(item => {
                                if (item.path) {
                                    let menuItem = {
                                        path: item.path.replace("/", ""),
                                        name: item.menuName,
                                        component: () => import('../views/' + item.pagePath + '.vue')
                                    };
                                   thirdMenuRoute.children.push(menuItem);
                                }
                            })
                            subMenuRoute.children.push(thirdMenuRoute);
                        }
                    })
                    manageRoute.children.push(subMenuRoute);
                }

            })
            console.log(manageRoute);
            router.addRoute(manageRoute);
        }
    }
}

setRoutes();


router.beforeEach((to, from, next) => {
    NProgress.start();
    localStorage.setItem("currentPathNames",JSON.stringify(to.matched.map((item)=>item.name)));
    store.commit("setPath");
   if(!to.matched.length){
       const menuList=localStorage.getItem("menuList");
       if(menuList){
           next("/404");
       }else{
           next("/login");
       }
   }else{
       next();
   }

})

router.afterEach((to,from)=>{
    NProgress.done();
    let isDynamicTitle=localStorage.getItem('configuration')?JSON.parse(localStorage.getItem('configuration')).isDynamicTitle:store.state.configuration.isDynamicTitle;
    if(isDynamicTitle) {
        document.title = to.name;
    }
})

export default router
